﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Spring.Expressions;

namespace Pg.BioMedics.SDR.Web.Controls
{
    /// <summary>
    /// This class holds properties mappings 
    /// utilized for properties binding from source and target view
    /// </summary>
    [Serializable]
    public class PropertyMapping
    {
        #region Fields

        private string source;
        private string target;

        private object value;

        #endregion

        #region Properties

        /// <summary>
        /// Gets / sets source property binding expression
        /// </summary>
        public string Source
        {
            get
            {
                return this.source;
            }

            set
            {
                this.source = value;
            }
        }

        /// <summary>
        /// Gets / sets target property binding expression
        /// </summary>
        public string Target
        {
            get
            {
                return this.target;
            }

            set
            {
                this.target = value;
            }
        }

        /// <summary>
        /// Gets extracted value
        /// </summary>
        internal object Value
        {
            get
            {
                return this.value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Extracts property value from source view according 
        /// to the mapping expression
        /// </summary>
        /// <param name="sourceView">Source view object</param>
        internal void ExtractValue(object sourceView)
        {
            this.value = ExpressionEvaluator.GetValue(sourceView, this.source);
        }

        /// <summary>
        /// Applies property value to target view according 
        /// to the mapping expression
        /// </summary>
        /// <param name="targetView">Target view object</param>
        internal void ApplyValue(object targetView)
        {
            ExpressionEvaluator.SetValue(targetView, this.target, this.value);
        }

        #endregion
    }
}
